<script>
import { mapStores } from 'pinia'
import { useBaseStore } from '../stores/useBaseStore.js'
import { loadUserMenus } from '../api/index.js'
import logoImg from '../assets/img/logo.png'
import userImg from '../assets/img/user.jpg'

export default {
  data() {
    return {
      logoImg,
      userImg,
      menus: [],
    }
  },
  computed: {
    ...mapStores(useBaseStore),
  },
  methods: {
    async initMenus() {
      let data = await loadUserMenus()
      // console.log(data);
      this.menus = data
      this.baseStore.menus = data
    },
    loginOut() {
      this.baseStore.clearLoginData()
      this.$router.push('/login')
    },
  },
  created() {
    this.initMenus()
  },
}
</script>
<template>
  <el-container class="home-box">
    <el-header>
      <div class="left">
        <el-avatar shape="square" :size="48" :src="logoImg" />
        <span class="title">课程管理系统</span>
      </div>
      <div class="right">
        <el-avatar :size="48" :src="userImg" />
        <el-dropdown>
          <div>
            <span>{{ baseStore.user.username }}</span>
            <el-icon>
              <ep-arrow-down />
            </el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="loginOut()">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
          background-color="#324157"
          text-color="#fff"
          active-text-color="#20a0ff"
          :default-active="$route.path"
          router
        >
          <el-menu-item v-for="m in menus" :key="m.right_id" :index="m.right_path">
            <el-icon v-html="m.right_icon"></el-icon>
            <span>{{ m.right_name }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.home-box {
  height: 100%;
}
.el-avatar {
  vertical-align: middle;
}
.title {
  font-size: 28px;
  color: white;
  vertical-align: middle;
  margin-left: 14px;
}
.el-dropdown {
  vertical-align: middle;
  padding: 0px 14px;
  color: white;
}
.el-header {
  background-color: #242f42;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-aside {
  background-color: #324157;
}
.el-icon {
  vertical-align: bottom;
}
.is-active :deep(svg *) {
  stroke: #20a0ff;
}
.el-main {
  background-color: #efefef;
}
</style>
